/**
 * Copyright 2005 The Apache Software Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.edi.exceptions;


public class EdiException extends Exception {

	private String errorInfo;

	/**
	 * 
	 */
	private static final long serialVersionUID = -7440897593078519525L;

	public EdiException() {
		super();
	}

	public EdiException(String message) {
		super(message);
		this.errorInfo = message;
	}

	public EdiException(String message, Throwable t) {
		super(message, t);
		this.errorInfo = message;
	}

	public String getMessage() {
		return getErrorMessage(this);
	}
	
	public String getErrorInfo(){
		return this.errorInfo;
	}

	private String getErrorMessage(Throwable selfCause) {
		StringBuffer causeInfo = new StringBuffer();
		StackTraceElement[] selfTraces = selfCause.getStackTrace();
		causeInfo.append(selfCause.getClass().getName()).append(":");
		if (selfCause instanceof EdiException) {
			if (((EdiException)selfCause).getErrorInfo() != null)
				causeInfo.append(((EdiException)selfCause).getErrorInfo()).append("\n");
			else
				causeInfo.append("\n");
		}else{
			if (selfCause.getMessage() != null)
				causeInfo.append(selfCause.getMessage()).append("\n");
			else
				causeInfo.append("\n");
		}
		for (StackTraceElement selfTace : selfTraces) {
			causeInfo.append("\tat ").append(selfTace).append("\n");
		}

		Throwable nestCause = selfCause.getCause();
		if (nestCause != null) {
			causeInfo.append("Caused by: ");
			causeInfo.append(getErrorMessage(nestCause));
		}
		return causeInfo.toString();
	}
}
